import { ref } from "vue";
import { EleMessage } from "ele-admin-plus";

/**
 * 使用模态框的钩子
 * @param {Object} options - 配置选项
 * @param {Function} [options.beforeOpen] - 弹框打开前执行
 * @param {Function} [options.afterOpen] - 弹框打开后执行
 * @returns {Object} - 返回当前数据、弹框可见状态及打开弹框的方法
 */
export const useModal = (options = {}) => {
  const current = ref(null);
  const visible = ref(false);

  const open = async (row = null) => {
    try {
      current.value = row;

      if (options.beforeOpen) {
        await options.beforeOpen(row);
      }

      console.log(current.value, "current.value"); // 打印当前数据 (men)
      visible.value = true;

      if (options.afterOpen) {
        options.afterOpen(row);
      }
    } catch (error) {
      EleMessage.error(error?.message);
      return Promise.reject(error);
    }
  };

  return {
    current,
    visible,
    open,
  };
};
